package com.foosun.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.foosun.core.base.PageData;
import com.foosun.core.entity.User;
import com.foosun.core.filter.SessionFilter;
import com.foosun.core.service.RoleService;
import com.foosun.core.service.UserService;
import com.foosun.core.util.EncryptUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Nelin on 14-6-6.
 */
@Controller
@RequestMapping("/common/user")
public class UserAction {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, PageData<User> pageData, HttpServletRequest request) throws Exception {
        pageData.setWebMap(request.getParameterMap());
        pageData = userService.findList(pageData);
        model.addAttribute("pageData", pageData);
        return "/common/user/list";
    }

    @RequestMapping("/list.flex")
    @ResponseBody
    public PageData _list(Model model, PageData<User> pageData, HttpServletRequest request) throws Exception {
        pageData.setWebMap(request.getParameterMap());
        pageData = userService.findList(pageData);
        System.out.println("user list session = " + request.getSession().getId());
        return pageData;
    }

    @RequestMapping("/new")
    public String form(Model model) throws Exception {
        commonData(model);
        return "/common/user/input";
    }

    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public String password() throws Exception {
        return "/password";
    }


    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable("id") Integer id) throws Exception {
        User user = userService.findById(id);
        model.addAttribute("entity", user);
        commonData(model);
        model.addAttribute("roles", roleService.findAll());
        return "/common/user/input";
    }

    @RequestMapping("/edit.flex")
    @ResponseBody
    public JSONObject _edit(Integer id) throws Exception {
        JSONObject data = new JSONObject();
        User user = userService.findById(id);
        data.put("entity", user);
        _commonData(data);
        return data;
    }


    private void commonData(Model model) throws Exception {
        model.addAttribute("roles", roleService.findAll());
    }

    private void _commonData(JSONObject data) throws Exception {
        data.put("roles", roleService.findAll());
    }

    @RequestMapping("/repeat/account")
    @ResponseBody
    public Boolean repeatAccount(User user) throws Exception {
        List<User> users = userService.findByProperty("user_name", user.getUserName());
        if (users != null && !users.isEmpty()) {
            if (!users.get(0).getId().equals(user.getId())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/reset/pwd/{id}")
    @ResponseBody
    public Boolean resetPwd(@PathVariable("id") Integer id) throws Exception {
        User user = userService.findById(id);
        if (user != null) {
            user.setPassword("666666");
            userService.update(user);
        }
        return true;
    }

    @RequestMapping("/save")
    public String save(Model model, User user) throws Exception {
        userService.save(user);
        return "redirect:/common/user/list";
    }

    @RequestMapping("/delete/{id}")
    public String delete(Model model, @PathVariable("id") Integer id) throws Exception {
        userService.delete(id);
        return "redirect:/common/user/list";
    }

    @RequestMapping("/delete.flex")
    @ResponseBody
    public Boolean _delete(Integer id) throws Exception {
        userService.delete(id);
        return true;
    }

    @RequestMapping("/validate/pwd")
    @ResponseBody
    public Boolean validatePwd(User user, HttpSession session) throws Exception {
        String pwd = EncryptUtils.md5(user.getPassword());
        User loginUser = (User) session.getAttribute(SessionFilter.USER);
        loginUser = userService.findById(loginUser.getId());
        if (loginUser.getPassword().equals(pwd)) {
            return true;
        } else {
            return false;
        }
    }


    @RequestMapping("/modify/pwd.flex")
    @ResponseBody
    public Boolean _modifyPwd(User user) throws Exception {
        User u = userService.findById(user.getId());
        if (u != null && StringUtils.isNotBlank(u.getPassword())) {
            u.setPassword(EncryptUtils.md5(user.getPassword()));
            userService.update(u);
        } else {
            return false;
        }
        return true;
    }

}
